package lee.vioson.ebook.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.ui.Constants
import lee.vioson.ebook.ui.navigate
import lee.vioson.ebook.viewmodels.FeedbackHistoryViewModel
import lee.vioson.remotedata.bean.Feedback

/**
 *Author:viosonlee
 *Date:2023/6/12
 *DESCRIPTION:
 */
@Composable
fun FeedbackHistory(
    navHostController: NavHostController,
    appContainer: AppContainer,
    viewModel: FeedbackHistoryViewModel = viewModel(factory = viewModelFactory {
        initializer {
            FeedbackHistoryViewModel(
                appContainer
            )
        }
    })
) {
    val state by viewModel.state.collectAsState()
    val feedbackList = viewModel.feedbackList.collectAsLazyPagingItems()
    Scaffold(
        topBar = {
            TopBar(
                title = stringResource(id = R.string.History),
                navHostController = navHostController
            )
        }
    ) { paddingValues ->
        LazyColumn(modifier = Modifier.padding(paddingValues),
            content = {
                items(feedbackList) { feedback ->
                    feedback?.let { fb ->
                        FeedbackHistoryItem(feedback = fb) {
                            navHostController.navigate(
                                AppDestinations.FEEDBACK_DETAIL,
                                mapOf(Constants.msgId to fb.id)
                            )
                        }
                    }
                }
            })
    }
}

@Composable
fun FeedbackHistoryItem(feedback: Feedback, onNavigation2Detail: () -> Unit) {
    Column(Modifier.fillMaxWidth()) {
        Row(
            Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp)
                .height(56.dp)
                .clickable {
                    onNavigation2Detail()
                },
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = feedback.isReply,
                fontSize = 14.sp,
                color = AppTheme.appColors.amountTextColor
            )
            Text(
                text = feedback.messageContent, fontSize = 14.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = AppTheme.appColors.whiteText80
            )
            Spacer(modifier = Modifier.weight(1f))
            Text(
                text = feedback.messageDateStr,
                fontSize = 14.sp,
                color = AppTheme.appColors.whiteText80
            )
        }
        Divider()
    }
}